﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DevOnServer
{
    public class CInventory
    {
        public CInventory(CPlayerObject player)
        {
            Owner = player;
        }

        public void moveItem(byte oldPos, byte newPos)
        {
            if (oldPos == newPos || oldPos >= 42 || newPos >= 42)
                return;

            CItem from = Slots[oldPos];
            CItem to = Slots[newPos];

            if (from != null)
            {
                if (to != null && to.Prop == from.Prop)
                {
                    int tempCount = from.Count;

                    if (tempCount > to.MissingCountToMaximum)
                        tempCount = to.MissingCountToMaximum;

                    deleteItem(oldPos, tempCount);
                    to.Count += tempCount;
                }
                else
                {
                    Slots[oldPos] = to;
                    Slots[newPos] = from;
                }

                _send(CPacketCreator.moveItem(Owner, oldPos, newPos));
            }
        }

        public void deleteItem(byte place, int count)
        {
            if (count > Slots[place].Count)
                count = Slots[place].Count;

            Slots[place].Count -= count;

            _send(CPacketCreator.updateItem(Owner, place, UpdateItemType.Count, count));

            if (Slots[place].Count <= 0)
                Slots[place] = null;
        }

        public CItem[] Slots = new CItem[73];

        public CPlayerObject Owner { get; private set; }

        private void _send(CPacket packet)
        {
            Owner.Player.Client.send(packet);
        }
    }

    public enum UpdateItemType
    {
        Count = 0x0000,
        Upgrade = 0x0003,
        EleUpgrade = 0x0004,
        Element = 0x0005,
        Sockets = 0x0006,
        Card = 0x0007,
        Ticket = 0x000b
    }
}